//
//  GetOrderViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/26.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "GetOrderViewController.h"
#import "UINavigationBar+BackgroundColor.h"//导航栏渐变
#import "GetOrderNavView.h"//导航栏View
#import "HADirect.h"//轮播
#import "MessageViewController.h"//消息Controller
#import "MineViewController.h"//我的Controller
#import "LoginWithPhoneViewController.h"//手机登录Controller
#import "GetOrderDetailViewController.h"//抢单详情Controller

#import "GetOrderHeaderView.h"//页眉
#import "GetOrderTableViewCell.h"//cell


#define BannerHeight (SCREEN_WIDTH / 375  * 170)

@interface GetOrderViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, HADirectActionDelegate>
@property (nonatomic, strong) HADirect *hadView;//轮播View
@end

@implementation GetOrderViewController
{
    GetOrderNavView *_navView;
    GetOrderHeaderView *_headerView;
    
    /**外卖 TableView*/
    UITableView *_takeOutTableView;
    /**跑腿 TableView*/
    UITableView *_errandsTableView;
    /**送货 TableView*/
    UITableView *_deliveryTableView;
    /**大件 TableView*/
    UITableView *_bigPieceTableView;
    
    
    CGFloat _alphaa;
    
}

 static NSString *const GetOrderTableViewCellID = @"GetOrderTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    //设置导航栏
    [self setNav];
    //设置页眉与ScrollView
    [self setHeaderViewAndScrollView];
    
    [self.view bringSubviewToFront:_navView];
    
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBar.hidden = YES;
    _navView.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidDisappear:(BOOL)animated{
    _navView.hidden = YES;
}

- (void)login{
    LoginWithPhoneViewController *login = [[LoginWithPhoneViewController alloc] init];
    login.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:login animated:YES];

}

- (void)alertShow:(NSString *)string{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"未登录" message:string preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        MineViewController *mine = [[MineViewController alloc] init];
        mine.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:mine animated:YES];
    }];
    [alert addAction:cancle];
    UIAlertAction *login = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self login];
    }];
    [alert addAction:login];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    _navView = [[[NSBundle mainBundle] loadNibNamed:@"GetOrderNavView" owner:nil options:nil] lastObject];
    _navView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 64);
    _navView.backgroundColor = RGBACOLOR(53, 161, 238, 0);
    _navView.title.hidden = YES;
    [self.view addSubview:_navView];
    __weak __typeof__ (self)weakSelf = self;
    _navView.NavClick = ^(int num){
        switch (num) {
            case 0:
                [weakSelf jumpToMine];
                break;
            case 1:
                [weakSelf jumpToMessage];
                break;
            default:
                break;
        }
    };
}
//导航栏颜色渐变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _takeOutTableView || scrollView == _errandsTableView || scrollView == _deliveryTableView || scrollView == _bigPieceTableView) {
        CGFloat offset = scrollView.contentOffset.y;
        CGFloat alpha = offset / (BannerHeight - 64);
        _navView.backgroundColor = RGBACOLOR(53, 161, 238, alpha);
        if (scrollView.contentOffset.y >= (BannerHeight - 64)) {
            _navView.title.hidden = NO;
        }else{
            _navView.title.hidden = YES;
        }
        if (scrollView.contentOffset.y >= BannerHeight - 64) {
            _headerView.frame = CGRectMake(0, - BannerHeight + 64, SCREEN_WIDTH, 64 + 44);
        }else {
            _headerView.frame = CGRectMake(0, - scrollView.contentOffset.y, SCREEN_WIDTH, BannerHeight + 44);
        }
        return;
    }

    if (scrollView == _scrollView) {
        CGFloat x = scrollView.contentOffset.x;
        if (x < 0) {
            _line.frame = CGRectMake(0, BannerHeight + 43, SCREEN_WIDTH / 4, 1);
        }else if (x > SCREEN_WIDTH * 3){
            _line.frame = CGRectMake(SCREEN_WIDTH / 4 * 3, BannerHeight + 43, SCREEN_WIDTH / 4, 1);
        }else _line.frame = CGRectMake(x / 4, BannerHeight + 43, SCREEN_WIDTH / 4, 1);
    }
}
- (void)jumpToMine{
    DLog(@"我的");
    [self alertShow:@"是否登录"];
//    MineViewController *mine = [[MineViewController alloc] init];
//    mine.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:mine animated:YES];
}
- (void)jumpToMessage{
    DLog(@"消息");
    MessageViewController *message = [[MessageViewController alloc] init];
    message.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:message animated:YES];
}

#pragma mark === 设置页眉 ScrollView ===
- (void)setHeaderViewAndScrollView{
    __weak __typeof__ (self) weakSelf = self;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 4, SCREEN_HEIGHT - 44);
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    _takeOutTableView = [self createTableViewAtIndex:0];
    _errandsTableView = [self createTableViewAtIndex:1];
    _deliveryTableView = [self createTableViewAtIndex:2];
    _bigPieceTableView = [self createTableViewAtIndex:3];
    
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"GetOrderHeaderView" owner:nil options:nil] lastObject];
    _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, BannerHeight);
    [self.view addSubview:_headerView];
    _headerView.buttonClick = ^(int num){
        weakSelf.line.frame = CGRectMake(num * (SCREEN_WIDTH / 4), BannerHeight + 43, SCREEN_WIDTH / 4, 1);
        weakSelf.scrollView.contentOffset = CGPointMake(num * SCREEN_WIDTH, 0);
    };
    
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, BannerHeight + 43, SCREEN_WIDTH / 4, 1)];
    _line.backgroundColor = APPCOLOR;
    [_headerView addSubview:_line];
    
    //设置轮播
    NSArray *arr = @[@"had1",@"had2",@"had3"];
    _hadView = [HADirect direcWithtFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 375 * 170) ImageArr:arr];
    [_headerView.bannerView addSubview:_hadView];
    _hadView.delegate = self;
    
    [_headerView setView];
}


#pragma mark === 轮播Delegate ===
- (void)HADirectActionWithIndex:(NSInteger)index{
    NSLog(@"index = %ld", index);
}


#pragma mark === 创建tableView ===
- (UITableView *)createTableViewAtIndex:(NSInteger)index{
    UIView *HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, BannerHeight + 44)];
    HeaderView.backgroundColor = [UIColor clearColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 44)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableHeaderView = HeaderView;
    tableView.tag = 2000 + index;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = RGBCOLOR(236, 236, 236);
//    tableView.bounces = NO;
    tableView.showsVerticalScrollIndicator = NO;
    [_scrollView addSubview:tableView];
    
    [tableView registerNib:[UINib nibWithNibName:@"GetOrderTableViewCell" bundle:nil] forCellReuseIdentifier:GetOrderTableViewCellID];
    return tableView;
}

#pragma mark === UITableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GetOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GetOrderTableViewCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GetOrderDetailViewController *getOrder = [[GetOrderDetailViewController alloc] init];
    getOrder.state = 0;
    getOrder.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:getOrder animated:YES];
}


#pragma mark === 设置tableView的偏移量 ===
-(void)setTableViewContentOffsetWithTag:(NSInteger)tag contentOffset:(CGFloat)offset{
    CGFloat tableViewOffset = offset;
    if(offset > BannerHeight){
        tableViewOffset = BannerHeight;
    }
    if (tag == 2000) {
        [_errandsTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_deliveryTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_bigPieceTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }else if(tag == 2001){
        [_takeOutTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_deliveryTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_bigPieceTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }else if (tag == 2002){
        [_takeOutTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_errandsTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_bigPieceTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }else{
        [_takeOutTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_errandsTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_deliveryTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:_scrollView]) {
        return;
    }
    [self setTableViewContentOffsetWithTag:scrollView.tag contentOffset:scrollView.contentOffset.y];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
